﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TriggerConsole
{
    class Program
    {
        static int _timeInterval = 5000;
        static private System.Windows.Forms.Timer xTimer = new System.Windows.Forms.Timer();
        static bool exitFlag = false;
        //static Thread ConsoleKeyListener = new Thread(new ThreadStart(ListerKeyBoardEvent));

        static string[] _products = { "ZZ1100704005018018", "ZZ1100704005025025" };
        static string[] _plant = { "2811",  "2811" };

        static void Main(string[] args)
        {
            xTimer.Tick += new EventHandler(TimerEventProcessor);

            xTimer.Interval = _timeInterval;
            StartTimer();
            //ConsoleKeyListener.Start();

            // Runs the timer, and raises the event. 
            while (exitFlag == false)
            {
                // Processes all the events in the queue.
                Application.DoEvents();
            }
            
        }

        static private void StartTimer()
        {
            xTimer.Start();
        }

        static private void StopTimer()
        {
            xTimer.Stop();
        }

        private static void TimerEventProcessor(Object myObject,
                                            EventArgs myEventArgs)
        {
            var rProduct = new Random();
            var rQty = new Random();
            int i = rProduct.Next(0, _products.Length);
            int qty = 100; //qty.Next(10);
            if (insertToConsole(_products[i], qty, _plant[i]) > 0)
            {
                Console.WriteLine("Insert Product " + _products[i] + ", Qtp = " + qty.ToString() + " Plant = " + _plant[i]);
            }
        }

        static public string ConnectionString
        {
            get
            {
                return @"Data Source=localhost\sqlexpress;
                		Initial Catalog=DBConsohTest;persist security info=True;user id=exportadmin;password=pclocl;";
            }
        }

        static private int insertToConsole(string productId, int qty, string plantCode)
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                
                string Command =  (@"INSERT INTO [dbo].[tbt_PalletDetail]
                                               ([PalletID]
                                               ,[ItemNo]
                                               ,[ProductCode]
                                               ,[Lot]
                                               ,[MachineCode]
                                               ,[ProductQty]
                                               ,[PutawayPlanQty]
                                               ,[PutawayQty]    
                                               ,[PlantCode]
                                                )
                                    VALUES
                                               ('301-1-311-140102-0001'
                                               ,'1'
                                               ,@productid
                                               ,1312
                                               ,'IF01'
                                               ,@qty
                                               ,null
                                               ,null
                                               ,@plantcode)");
                SqlCommand CurrentCommand = new SqlCommand(Command, connection);
                CurrentCommand.Parameters.AddWithValue("productid", productId);
                CurrentCommand.Parameters.AddWithValue("qty", qty);
                CurrentCommand.Parameters.AddWithValue("plantcode", plantCode);

                try
                {
                    connection.Open();
                    return CurrentCommand.ExecuteNonQuery();
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.Message);
                    return -1;
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        //public static void ListerKeyBoardEvent()
        //{
        //    do
        //    {
        //        if (Console.ReadKey(true).Key == ConsoleKey.Escape)
        //        {
        //            //xTimer.Enabled = false;
        //            StopTimer();
        //            Console.WriteLine("\nPress the 'c' to continue.");
        //            //exitFlag = true;
        //        }
        //        else if (Console.ReadKey(true).Key == ConsoleKey.C)
        //        {
        //            //xTimer.Enabled = true;
        //            StartTimer();
        //            //exitFlag = false;
        //            Application.DoEvents();
        //        }
        //    } while (true);
        //}

    }
}
